package migrate

import (
	"mini-app/config/mysql"
	"mini-app/internal/models"

	"192.168.1.75/go-pkg/helper"

	"gorm.io/gorm"
)

type MigrateFunc struct{}

func (MigrateFunc) Script202407() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{})
	tx.AutoMigrate(&models.User{})
	tx.AutoMigrate(&models.ShoppingCart{})
	tx.AutoMigrate(&models.GoodsClass{})
	tx.AutoMigrate(&models.GoodsSetting{})
	tx.Exec("alter table goods_setting drop primary key,add primary key(`robot_id`,`goods_id`)")
	tx.AutoMigrate(&models.Order{})
	tx.Migrator().DropColumn(&models.GoodsSetting{}, "goods_name")
	tx.Migrator().DropColumn(&models.GoodsSetting{}, "goods_type")
	tx.Migrator().CreateIndex(&models.Order{}, "idx_openid_goodsID")
	return nil
}
func (MigrateFunc) Script202409() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响
	tx.Migrator().AutoMigrate(&models.ObjAssociation{})                                                                             // 对象关联表
	tx.Migrator().AutoMigrate(&models.PromotionChannel{})                                                                           // 推广渠道
	tx.Migrator().AddColumn(&models.User{}, "channel_id")                                                                           // 用户渠道id
	return nil
}

func (MigrateFunc) Script202502() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响
	tx.Migrator().AutoMigrate(&models.Advertisement{})                                                                              // 对象关联表
	return nil
}
func (MigrateFunc) Script202505() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响
	tx.Migrator().AutoMigrate(&models.Advertisement{})
	tx.Migrator().AddColumn(&models.Advertisement{}, "paid_id")
	tx.Migrator().AddColumn(&models.Advertisement{}, "discount_amount")
	tx.Migrator().AutoMigrate(&models.AdConfig{})
	tx.Migrator().AutoMigrate(&models.AdCoupon{})
	tx.Migrator().AutoMigrate(
		&models.OnlineEvent{},
		&models.OnlineEventUser{},
		&models.OnlineEventSupportLog{},
		&models.User{},
		&models.UserAddress{},
		&models.Announcement{},
		&models.Order{},
		&models.GoodsReview{},
		&models.GoodsReviewAuditLog{},
		&models.Banner{},
	)
	return nil
}
func (MigrateFunc) Script202507() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响
	tx.Migrator().AddColumn(&models.AdCoupon{}, "location_ids")
	tx.Migrator().AddColumn(&models.AdCoupon{}, "status")
	tx.Migrator().AddColumn(&models.AdCoupon{}, "device_group_id")
	tx.Migrator().AutoMigrate(&models.AdCoupon{})

	tx.Migrator().AutoMigrate(&models.PublishDevice{})
	tx.Migrator().AutoMigrate(&models.PublishDeviceGroup{})
	return nil
}

func (MigrateFunc) Script202508() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响

	err := tx.Migrator().AutoMigrate(
		&models.PuppetUser{},
		&models.OnlineEvent{},
		&models.OnlineEventUser{})

	helper.Must(err)

	return nil
}
func (MigrateFunc) Script202510() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci").Session(&gorm.Session{}) // 安全会话，后面互不影响

	tx.Migrator().AutoMigrate(&models.AdRefund{})
	tx.Migrator().AddColumn(&models.AdRefund{}, "user_id")
	tx.Migrator().AddColumn(&models.AdRefund{}, "user_name")
	tx.Migrator().AddColumn(&models.Advertisement{}, "schedule_id")
	tx.Migrator().AddColumn(&models.Advertisement{}, "schedule_status")
	tx.Migrator().AddColumn(&models.Advertisement{}, "support_activity_id")
	err := tx.Migrator().AutoMigrate(
		&models.Advertisement{})

	helper.Must(err)

	return nil
}
